from selenium.webdriver.common.by import By
from common.Utils import BlogDriver
import time

class BlogLogin:
    driver=""
    url=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        # 设置 隐式等待
        self.driver.implicitly_wait(5)
        #访问url
        self.driver.get(self.url)

    #封装“清空 输入框内容”函数
    def InputClear(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

    #测试 [登录成功] 用例
    def loginSucTest(self):
        #time.sleep(2)
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #检查是否进入博客主页
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title" )
        #屏幕截图
        BlogDriver.getScreenShot()
        time.sleep(2)

    # 测试 [登录失败] 用例
    def loginFailTest(self):
        #（1）错误的用户名,正确的密码
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("listtt")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        expect="用户不存在"
        alert=self.driver.switch_to.alert
        alert_text=alert.text
        print(alert_text)
        assert expect==alert_text
        alert.accept()
        BlogDriver.getScreenShot()

        #（2）正确的用户名，错误的密码
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        expect = "密码错误"
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(alert_text)
        assert expect == alert_text
        alert.accept()
        BlogDriver.getScreenShot()

        #（3）正确的用户名，但密码为空
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        expect = "账号或密码不能为空"
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(alert_text)
        assert expect == alert_text
        alert.accept()
        BlogDriver.getScreenShot()

        #（4）正确的密码，但用户名为空
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        expect = "账号或密码不能为空"
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(alert_text)
        assert expect == alert_text
        alert.accept()
        BlogDriver.getScreenShot()

        #（5）用户名，密码均为空
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        expect = "账号或密码不能为空"
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(alert_text)
        assert expect == alert_text
        alert.accept()
        BlogDriver.getScreenShot()




